package com.tms.app.tms.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tms.app.tms.bean.UpdateInfo;
import com.tms.app.tms.bean.UserInfo;
import com.tms.app.tms.bean.response.BaseResponse;
import com.tms.app.tms.ui.AboutMeActivity;
import com.tms.app.tms.ui.base.presenter.ComPresenter;
import com.tms.app.tms.ui.base.ui.BaseActivity;
import com.tms.app.tms.R;
import com.tms.app.tms.ui.login.LoginAt;
import com.tms.app.tms.utils.ApkUpdate;
import com.tms.app.tms.utils.DataCleanManager;
import com.tms.app.tms.utils.SharePreferenceUtil;
import com.tms.app.tms.widget.CommonDialog;
import com.tms.app.tms.widget.MyDialogSelect;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingHomeActivity extends BaseActivity {

    @BindView(R.id.text_account)
    TextView textAccount;
    @BindView(R.id.parent_seting_account)
    RelativeLayout parentSetingAccount;
    @BindView(R.id.text_checkbox_msg)
    CheckBox textCheckboxMsg;
    @BindView(R.id.parent_setting_msg)
    RelativeLayout parentSettingMsg;
    @BindView(R.id.text_clear)
    TextView textClear;
    @BindView(R.id.parent_seting_clear)
    RelativeLayout parentSetingClear;
    @BindView(R.id.parent_part_1)
    LinearLayout parentPart1;
    @BindView(R.id.parent_setting_about)
    RelativeLayout parentSettingAbout;
    @BindView(R.id.text_versions)
    TextView textVersions;
    @BindView(R.id.parent_setting_update)
    RelativeLayout parentSettingUpdate;
    @BindView(R.id.text_tel)
    TextView textTel;
    @BindView(R.id.parent_setting_call)
    RelativeLayout parentSettingCall;
    @BindView(R.id.parent_part_2)
    LinearLayout parentPart2;
    @BindView(R.id.button_send)
    Button buttonSend;
    private UserInfo userInfo;
    private CommonDialog dialog;
    private CommonDialog commonDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_home);
        ButterKnife.bind(this);
        setToolBarTitle("设置", -1, 0);
        textClear.setText(DataCleanManager.getTotalCacheSize(this));
        userInfo = SharePreferenceUtil.getObject(this, UserInfo.class);
        if (userInfo!=null&&userInfo.getMobile()!=null){
            textAccount.setText(userInfo.getMobile());
        }
    }



    @OnClick({R.id.parent_seting_clear,R.id.parent_setting_call,R.id.parent_setting_update,R.id.parent_setting_about,R.id.button_send})
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case R.id.parent_setting_call:
                myDialogSelect = new MyDialogSelect(this, this, "", "");
                myDialogSelect.show();
                break;
            case R.id.dialog_button_2:
                myDialogSelect.close();
                callPhone("400-3333-000");
                break;
            case R.id.dialog_button_1:
                myDialogSelect.close();

                break;
            case R.id.parent_seting_clear:
                DataCleanManager.clearAllCache(this);
                break;
            case R.id.parent_setting_about:
                    startActivity(new Intent(this, AboutMeActivity.class));
                break;
            case R.id.parent_setting_update:
                ApkUpdate.getUpdateInfo();
                break;
            case R.id.button_send:
                commonDialog= CommonDialog.getInstance(this, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new ComPresenter().loginOut(token);
                        commonDialog.close();
                    }
                },"确定要退出登录吗");
                commonDialog.show();
                break;
            case R.id.dialog_photo_button_2:
                dialog.close();
                break;
        }
    }



    @Subscribe(threadMode = ThreadMode.MAIN)
    public void checkUpdateInfoStatus(String json){
        BaseResponse baseResponse=gson.fromJson(json,BaseResponse.class);
        if (baseResponse.getCode()==200){
            Intent intent = new Intent(this, LoginAt.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            clearLocalUserData();
            finish();
        }
    }



}
